var inte;
	$(function(){
		 $("#QRform").validate({
				rules : {
					number : {
						required : true,
						digits : true,
						min : 1,
						max:5000000
					},
			 		money :{
			 			number:true,
			 			min : 1,
			 			max : 4999
			 		}
				},
				messages : {
					number : {
						required : "请输入生成数量",
						digits : "生成数量必须为正整数",
						min : "生成数量必须大于0",
						max : "生成数量最大为500万"
					},
			 		money :{
			 			number:"请输入数字",
			 			min : "红包金额大于等于1元",
			 			max : "红包金额小于等于4999元"
			 		}
				}
			});
		 
		 
//		 $("#createCode_img").click(function(){
//				$("#size").parent().parent().css({'display':'block'});
//				$("#addNum").css({'display':'none'});
//				$("#numberMsg").html('<i class="fa fa-info-circle"></i> 二维码图片数量最大为10万');
//				$('#createCode').removeClass('btn-primary');
//				$(this).addClass('btn-primary');
//				$("#createBtn").attr("onclick","createQRImg()");
//			});
//			$('#createCode').click(function(){
//				$("#addNum").css({'display':'block'});
//				$("#size").parent().parent().css({'display':'none'});
//				$("#numberMsg").html('');
//				$('#createCode_img').removeClass('btn-primary');
//				$(this).addClass('btn-primary');
//				$("#createBtn").attr("onclick","createQR()");
//			});
	})
	
	function checkMoney(){
		var money = $('#money').val();
		if(money == "" || money == "undefined"){
			return true;
		}
		var list = money.split(".");
		if(list.length>1 && list[1].length > 2){
			layer.msg("红包金额只能输入两位小数");
			return false;
		}
		return true;
	}
	
	function createQR(){
		if (!$("#QRform").valid()){
			return;
		}
		if(!checkMoney()){
			return;
		}

		var index = layer.open({
            type: 1,
            title: '正在产码，请稍候',
            area: ['500px', '100px'],
            shadeClose: false, //点击遮罩关闭
			closeBtn : 0,
            content:'<div class="ibox-content form-horizontal">\
            			<div class="form-group" id="alertMessage">\
            				<div class="progress progress-striped active" >\
            					<div style="width: 1%" aria-valuemax="100" aria-valuemin="0" aria-valuenow="75" role="progressbar" class="progress-bar progress-bar-primary">'
    	    						+'<span class="sr-only">\
    	    						</span>\
    	    					</div>\
    	    				</div>\
    	    			</div>\
    	    		</div>'  	    
        });
		$("<div id = 'background'>").appendTo(window.parent.document.body).css({//添加一个遮罩
		"width" : "100%",
		"z-index" : "99999",
		"height" : "100%",
		"background-image" : "url('../../img/null.gif')",
		"position" : "absolute",
		"left" : "0px",
		"top" : "0px"
		});
		window.onbeforeunload = function(){
			return "当前有任务在执行,你确定要离开这个页面吗";
		}
		window.setTimeout(goon, 1000)
		
	}
	

	
	function changeProcessbar(id){
		var number = $("#number").val();
		 $.ajax({
				type : "POST",
				url : "getProgress",
				data : {'id':id},
				success:function(data){
					//console.log(data);
					if(data != 'null'){
						data = Math.round(data*100/number);
						if(data > 0 && data < 100){
							$(".progress-bar").width(data+"%");							
						}else if(data >= 100){
							window.clearInterval(inte);
							$(".progress-bar").width("100%");
						}
					}
				}
		 });
		
	}
	
	function goon() {
		var number = $('#number').val().trim();
		var money = $('#money').val();
		var url = $('#url option:selected').val().trim();
		var addNum = $("input[name='addNum']:checked").val();
		var code = $('#code option:selected').val().trim();
		var id = Math.uuidFast();
		 $.ajax({
			cache : true,
			type : "POST",
			url : "qrCodeAdd",
			timeout : 1000*60*10,//超时时间3min
			data : {'number':number,'url':url,'addNum':addNum,'id':id,"code":code,"money":money},// 你的formid
			beforeSend:function(){
				inte =window.setInterval(changeProcessbar.bind(null,id),2000);
			},
			error : function(request) {
				$("#background",window.parent.document).remove();//清除遮罩
				window.onbeforeunload=null;
				window.clearInterval(inte);
				document.getElementById("alertMessage").innerHTML = "Connection error！";
				window.setTimeout(function(){
					layer.closeAll();
				}, 2000);
			},
			success : function(data) {
				//console.log(data);
				$("#background",window.parent.document).remove();//清除遮罩
				window.onbeforeunload = null;
				window.clearInterval(inte);
				var reg = /[.zip]/;
				if (data == null) {
					document.getElementById("alertMessage").innerHTML = "Connection error！";
				}else{
					if(reg.test(data)){						
						$(".progress-bar").width("100%");
						window.location.href = ctx+"/page/download.jsp?fileName="+data;				
					}else{						
						document.getElementById("alertMessage").innerHTML = "<font color='red'>"+data+"</font>";
					}
				}				
				layer.closeAll();	
			}
		})
	} 
	
	
	function  checkSize(){
		var size=$('#size').val().trim();
		if(size == "" || size =="undefined"){
			layer.msg('请输入图片大小');
			return false;
		}
		if(isNaN(size)){
			layer.msg('请输入正确的图片大小');
			return false;				
		}
		return true;			
	}
	
	function createQRImg(){
		if (!$("#QRform").valid()){
			return;
		}
		if(!checkSize()){
			return;
		}
		var number = $('#number').val().trim();
		if(number > 100000){
			layer.msg("生成二维码图片数量不能超过10W");
			return;
		}
		
		var index = layer.open({
            type: 1,
            title: '正在产码，请稍候',
            area: ['500px', '100px'],
            shadeClose: false, //点击遮罩关闭
			closeBtn : 0,
            content:'<div class="ibox-content form-horizontal">\
            			<div class="form-group" id="alertMessage">\
            				<div class="progress progress-striped active" >\
            					<div style="width: 1%" aria-valuemax="100" aria-valuemin="0" aria-valuenow="75" role="progressbar" class="progress-bar progress-bar-primary">'
    	    						+'<span class="sr-only">\
    	    						</span>\
    	    					</div>\
    	    				</div>\
    	    			</div>\
    	    		</div>'  	    
        });
		$("<div id = 'background'>").appendTo(window.parent.document.body).css({//添加一个遮罩
		"width" : "100%",
		"z-index" : "99999",
		"height" : "100%",
		"background-image" : "url('../../img/null.gif')",
		"position" : "absolute",
		"left" : "0px",
		"top" : "0px"
		});
		window.onbeforeunload = function(){
			return "当前有任务在执行,你确定要离开这个页面吗";
		}
		window.setTimeout(goonImg, 1000);
		
	}
	
	function goonImg(){
		var number = $('#number').val().trim();
		var url = $('#url option:selected').val().trim();
		var id = Math.uuidFast();
		var size = $('#size').val().trim();
		 $.ajax({
			cache : true,
			type : "POST",
			url : "createQrcodeImg",
			timeout : 1000*60*3,//超时时间3min
			data : {'number':number,'url':url,'id':id,'size':size},// 你的formid
			beforeSend:function(){
				inte =window.setInterval(changeProcessbar.bind(null,id),2000);
			},
			error : function(request) {
				$("#background",window.parent.document).remove();//清除遮罩
				window.onbeforeunload=null;
				window.clearInterval(inte);
				document.getElementById("alertMessage").innerHTML = "Connection error！";
				window.setTimeout(function(){
					layer.closeAll();
				}, 2000);
			},
			success : function(data) {
				//console.log(data);
				$("#background",window.parent.document).remove();//清除遮罩
				window.onbeforeunload = null;
				var reg = /[.zip]/;
				if (data == null) {
					window.clearInterval(inte);
					document.getElementById("alertMessage").innerHTML = "Connection error！";
				}else{
					if(reg.test(data)){
						window.clearInterval(inte);
						$(".progress-bar").width("100%");
						window.location.href = ctx+"/page/download.jsp?fileName="+data;				
					}else{
						window.clearInterval(inte);
						document.getElementById("alertMessage").innerHTML = "<font color='red'>"+data+"</font>";
					}
				}
				window.setTimeout(function(){
					layer.closeAll();
				}, 2000);
				
			}
		})
	}